home *** CD-ROM | disk | FTP | other *** search
/ Chip 2007 January, February, March & April / Chip-Cover-CD-2007-02.iso / Pakiet bezpieczenstwa / mini Pentoo LiveCD 2006.1 / mpentoo-2006.1.iso / livecd.squashfs / usr / lib / python2.4 / test / test_thread.pyc (.txt) < prev    next >
Python Compiled Bytecode  |  2005-10-18  |  3KB  |  126 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.4)
  3.  
  4. from test.test_support import verbose
  5. import random
  6. import thread
  7. import time
  8. mutex = thread.allocate_lock()
  9. rmutex = thread.allocate_lock()
  10. running = 0
  11. done = thread.allocate_lock()
  12. done.acquire()
  13. numtasks = 10
  14.  
  15. def task(ident):
  16.     global running
  17.     rmutex.acquire()
  18.     delay = random.random() * numtasks
  19.     rmutex.release()
  20.     if verbose:
  21.         print 'task', ident, 'will run for', round(delay, 1), 'sec'
  22.     
  23.     time.sleep(delay)
  24.     if verbose:
  25.         print 'task', ident, 'done'
  26.     
  27.     mutex.acquire()
  28.     running = running - 1
  29.     if running == 0:
  30.         done.release()
  31.     
  32.     mutex.release()
  33.  
  34. next_ident = 0
  35.  
  36. def newtask():
  37.     global next_ident, running
  38.     mutex.acquire()
  39.     next_ident = next_ident + 1
  40.     if verbose:
  41.         print 'creating task', next_ident
  42.     
  43.     thread.start_new_thread(task, (next_ident,))
  44.     running = running + 1
  45.     mutex.release()
  46.  
  47. for i in range(numtasks):
  48.     newtask()
  49.  
  50. print 'waiting for all tasks to complete'
  51. done.acquire()
  52. print 'all tasks done'
  53.  
  54. class barrier:
  55.     
  56.     def __init__(self, n):
  57.         self.n = n
  58.         self.waiting = 0
  59.         self.checkin = thread.allocate_lock()
  60.         self.checkout = thread.allocate_lock()
  61.         self.checkout.acquire()
  62.  
  63.     
  64.     def enter(self):
  65.         checkin = self.checkin
  66.         checkout = self.checkout
  67.         checkin.acquire()
  68.         self.waiting = self.waiting + 1
  69.         if self.waiting == self.n:
  70.             self.waiting = self.n - 1
  71.             checkout.release()
  72.             return None
  73.         
  74.         checkin.release()
  75.         checkout.acquire()
  76.         self.waiting = self.waiting - 1
  77.         if self.waiting == 0:
  78.             checkin.release()
  79.             return None
  80.         
  81.         checkout.release()
  82.  
  83.  
  84. numtrips = 3
  85.  
  86. def task2(ident):
  87.     global running
  88.     for i in range(numtrips):
  89.         if ident == 0:
  90.             delay = 0.001
  91.         else:
  92.             rmutex.acquire()
  93.             delay = random.random() * numtasks
  94.             rmutex.release()
  95.         if verbose:
  96.             print 'task', ident, 'will run for', round(delay, 1), 'sec'
  97.         
  98.         time.sleep(delay)
  99.         if verbose:
  100.             print 'task', ident, 'entering barrier', i
  101.         
  102.         bar.enter()
  103.         if verbose:
  104.             print 'task', ident, 'leaving barrier', i
  105.             continue
  106.     
  107.     mutex.acquire()
  108.     running -= 1
  109.     finished = running == 0
  110.     mutex.release()
  111.     if finished:
  112.         done.release()
  113.     
  114.  
  115. print '\n*** Barrier Test ***'
  116. if done.acquire(0):
  117.     raise ValueError, "'done' should have remained acquired"
  118.  
  119. bar = barrier(numtasks)
  120. running = numtasks
  121. for i in range(numtasks):
  122.     thread.start_new_thread(task2, (i,))
  123.  
  124. done.acquire()
  125. print 'all tasks done'
  126.